package com.zc.im.controller;

import com.alibaba.fastjson.JSONObject;
import com.zc.im.model.customer.ZcCustomerService;
import com.zc.im.model.customer.ZcCustomerServiceRelation;
import com.zc.im.model.user.ZcUser;
import com.zc.im.service.customer.CustomerService;
import com.zc.im.service.customer.CustomerServiceRelationService;
import com.zc.im.service.user.UserService;
import com.zc.im.util.JsonUtils;
import com.zc.im.util.ResponseJson;
import com.zc.im.util.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 *
 *客户对应的客服人员Controller
 * Created by wangcc on 2017/7/10.
 */
@RequestMapping(value = "/customer")
@Controller
public class ZcCustomerServiceRelationController
{

    private static Logger logger = Logger.getLogger(ZcCustomerServiceRelationController.class);
    @Autowired
    private CustomerServiceRelationService customerServiceRelationService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private UserService userService;

    /**
     * 查询客户对应的客服
     * 传入json{"uid":"用户id"}
     * @param customer
     * @return
     */
    @RequestMapping(value = "/getcustomer",method = RequestMethod.POST)
    public ResponseJson insertGroupAudit(String customer) {
        Integer key = null;
        ZcUser zcUserModel1 = new ZcUser();
        ZcCustomerService zcCustomerServiceModel = null;
        ZcCustomerServiceRelation customerServiceRelation = null;
        ZcCustomerService zcCustomerServiceMode2 = new ZcCustomerService();
        ZcCustomerServiceRelation zcCustomerServiceRelation = new ZcCustomerServiceRelation();
        Map<String, Object> dataMap = new HashMap<>();
        ResponseJson responseJson = new ResponseJson();
        if (StringUtils.isEmpty( customer )) {
            return responseJson;
        }
            JSONObject jsonType = JsonUtils.strToJsonObj(customer);
            //得到用户登录信息
            ZcUser zcUser = userService.queryUser(Integer.parseInt(jsonType.getString("uid")));
            zcCustomerServiceRelation.setUserId( zcUser.getUid() );      ;
            if (zcUser.getType()==2){
             //客服服务类型  个人 9= people
                zcCustomerServiceRelation.setCustomerServiceType("people");
            }else  if(zcUser.getType()==1){
             //企业 10=company
                zcCustomerServiceRelation.setCustomerServiceType("company");
            }
            //根据user_id查找对应客服
            try {
             customerServiceRelation = customerServiceRelationService.findCustomerServiceRelation( zcCustomerServiceRelation );
            } catch (Exception e) {
              logger.error(e.getMessage(), e );
        }
        //判断有无对应的客服关系
        if( customerServiceRelation == null ) {
            try {
                zcCustomerServiceModel = customerService.findCustomerService( zcCustomerServiceRelation.getCustomerServiceType() );
                //更新客服服务人数加1
                zcCustomerServiceMode2.setServiceUserNum( zcCustomerServiceModel.getServiceUserNum() + 1 );
                //更新客服id
                zcCustomerServiceMode2.setId(zcCustomerServiceModel.getId());
                //更新操作
                customerService.updateCustomerService( zcCustomerServiceMode2 );
            } catch (Exception e ) {
                logger.error(e.getMessage(), e );
            }
                if ( zcCustomerServiceModel.getUserId() != null ) {
                    //加入客服id
                    zcCustomerServiceRelation.setCustomerServiceUserId(zcCustomerServiceModel.getUserId());
                    //加入添加客服的时间
                    Date date = new Date();
                    Timestamp timeStamp = new Timestamp(date.getTime());
                    zcCustomerServiceRelation.setCreatedTime(timeStamp);
                    try {
                        key=customerServiceRelationService.insertCustomerServiceRelation( zcCustomerServiceRelation );
                        zcUserModel1=userService.queryUser( zcCustomerServiceModel.getUserId() );
                    }catch (Exception e){
                        logger.error(e.getMessage(),e);
                    }
                }
            dataMap.put("zcUserModel1", JsonUtils.objToStr( zcUserModel1 ) );
            responseJson.setDataMap(dataMap);
         } else {
            try {
                zcUserModel1=userService.queryUser( customerServiceRelation.getCustomerServiceUserId() );
            } catch (Exception e ) {
                logger.error(e.getMessage(), e );
            }
            dataMap.put( "zcUserModel1", JsonUtils.objToStr( zcUserModel1 ));
            responseJson.setDataMap( dataMap );
        }
            return responseJson;
    }
}
